﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeDelay.logHelp
{
    public class LogHelper
    {
        private object lockObj = new object();
        private static LogHelper instance;
        public static LogHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LogHelper();
                }
                return instance;
            }
        }
        public void writeLog(string msg)
        {
            lock(lockObj)
            {
                try
                {
                    string dir = System.Environment.CurrentDirectory + "\\log";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    //判断文件是否存在，不存在则创建的存在
                    string sPath = dir;
                    string date = System.DateTime.Now.ToString("yyyy-MM-dd");
                    sPath = Path.Combine(dir, date + "-task.txt");
                    //System.Windows.Forms.MessageBox.Show("sPath:" + sPath);
                    if (!File.Exists(sPath))
                    {
                        File.Create(sPath);
                    }
                    StreamWriter swb = new StreamWriter(sPath, true);
                    swb.WriteLine(System.DateTime.Now + " " + msg);
                    swb.Close();
                }
                catch (Exception ex)
                {
                    //System.Windows.Forms.MessageBox.Show("writeLog error:"+ ex.ToString());
                }
            }
            
        }





        public void writeProblemLog(string msg)
        {
            lock (lockObj)
            {
                try
                {
                    string dir = System.Environment.CurrentDirectory + "\\log";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    //判断文件是否存在，不存在则创建的存在
                    string sPath = dir;
                    string date = System.DateTime.Now.ToString("yyyy-MM-dd");
                    sPath = Path.Combine(dir, date + "-outboundproblem.txt");
                    //System.Windows.Forms.MessageBox.Show("sPath:" + sPath);
                    if (!File.Exists(sPath))
                    {
                        FileStream fs = File.Create(sPath);
                        fs.Close();
                    }
                    StreamWriter swb = new StreamWriter(sPath, true);
                    swb.WriteLine(msg);
                    swb.Close();
                }
                catch (Exception ex)
                {
                    //System.Windows.Forms.MessageBox.Show("writeLog error:"+ ex.ToString());
                }
            }

        }



        public void writeNoRoadMapBarcode(string msg)
        {
            try
            {
                string dir = System.Environment.CurrentDirectory + "\\log";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                //判断文件是否存在，不存在则创建的存在
                string sPath = dir;
                string date = System.DateTime.Now.ToString("yyyy-MM-dd");
                sPath = Path.Combine(dir, date + "-noroadmap.txt");
                //System.Windows.Forms.MessageBox.Show("sPath:" + sPath);
                if (!File.Exists(sPath))
                {
                    FileStream fs = File.Create(sPath);//创建文件
                    fs.Close();
                }
                StreamWriter swb = new StreamWriter(sPath, true);
                swb.Write(msg);
                swb.Close();
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("writeLog error:"+ ex.ToString());
            }

        }


        public void writeStaticBarcode(ArrayList arListStatics)
        {
            try
            {
                string dir = System.Environment.CurrentDirectory + "\\log";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                //判断文件是否存在，不存在则创建的存在
                string sPath = dir;
                string date = System.DateTime.Now.ToString("yyyy-MM-dd");
                sPath = Path.Combine(dir, date + "-static.txt");
                //System.Windows.Forms.MessageBox.Show("sPath:" + sPath);
                if (!File.Exists(sPath))
                {
                    FileStream fs = File.Create(sPath);//创建文件
                    fs.Close();
                }
                StreamWriter swb = new StreamWriter(sPath, true);
                foreach(string strLine in arListStatics)
                {
                    swb.WriteLine(strLine);
                }
                
                swb.Close();
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("writeLog error:"+ ex.ToString());
            }

        }


        public void writeConfig(string configFile,ArrayList arListConfig)
        {
            try
            {
                string dir = System.Environment.CurrentDirectory + "\\config";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                //判断文件是否存在，不存在则创建的存在
                string sPath = dir;
                sPath = Path.Combine(dir, configFile);
                if (!File.Exists(sPath))
                {
                    FileStream fs = File.Create(sPath);
                    fs.Close();
                }
                StreamWriter swb = new StreamWriter(sPath, false);
                foreach(string strLine in arListConfig)
                {
                    swb.WriteLine(strLine);
                }
                //logHelp.LogHelper.Instance.writeLog("writeConfig:"+ configFile+" 个数 " + arListConfig.Count.ToString());
                swb.Close();
            }
            catch (Exception ex)
            {

            }
        }

        public void writeRoomConfig(string configFile,string content)
        {
            try
            {
                string dir = System.Environment.CurrentDirectory + "\\config\\roomwechat";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                //判断文件是否存在，不存在则创建的存在
                string sPath = dir;
                sPath = Path.Combine(dir, configFile);
                if (!File.Exists(sPath))
                {
                    FileStream fs= File.Create(sPath);
                    fs.Close();
                }
                StreamWriter swb = new StreamWriter(sPath, false);
                swb.WriteLine(content);

                swb.Close();

            }
            catch (Exception ex)
            {

            }
        }


    }
}
